<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <meta name="layout" content="grame"/>
    <title>系统日志管理</title>
</head>

<body>
<div class="container">
    <div class="panel">
        更改后即时生效（重启后还原）。保存后永久生效。
    </div>

    <div class="row">
        <div class="form-inline">
            <label for="app_logger">App Logger</label>
            <select id="app_logger" onchange="setLevel(this.value)">
                <option selected>Level</option>
                <option value="ERROR">ERROR</option>
                <option value="INFO">INFO</option>
                <option value="DEBUG">DEBUG</option>
                <option value="TRACE">TRACE</option>
            </select>
        </div>
    </div>

    <div class="row">
        <div class="form-inline">
            <label for="hibernate_logger">Hibernate Logger</label>
            <select id="hibernate_logger" onchange="setHibernateLevel(this.value)">
                <option selected>Level</option>
                <option value="ERROR">ERROR</option>
                <option value="INFO">INFO</option>
                <option value="DEBUG">DEBUG</option>
                <option value="TRACE">TRACE</option>
            </select>
        </div>
    </div>

    <div class="row">
        <button onclick="test()">测试</button>
        <button onclick="save()">保存</button>
    </div>
</div>
<asset:javascript src="jquery"/>
<script>
    $(function () {
        getLevel();
    });

    function getLevel() {
        console.log("do function getLevel()");
        $.ajax({
            url: '${createLink(controller: 'logger',action: 'show')}',
            data: {
                logger: 'grame'
            },
            success: function (data) {
                var level = data.level;
                $("#app_logger").val(level);
            }
        });
        $.ajax({
            url: '${createLink(controller: 'logger',action: 'show')}',
            data: {
                logger: 'org.hibernate.SQL'
            },
            success: function (data) {
                var level = data.level;
                $("#hibernate_logger").val(level);
            }
        });
    }

    function setLevel(level) {
        console.log("do function setLevel(" + level + ")");
        update('conf', level);
        update('grails.app.controllers', level);
        update('grails.app.domain', level);
        update('grails.app.services', level);
        update('grails.app.filters', level);
        update('grame', level);
        update('com.springca', level);
        if (level == 'DEBUG') {
            update('grails.app.taglib', 'INFO');
        }
    }
    function setHibernateLevel(level) {
        update('org.hibernate.SQL', level);
        if (level == 'DEBUG') {
            update('org.hibernate.type.descriptor.sql.BasicBinder', 'TRACE');
        } else {
            update('org.hibernate.type.descriptor.sql.BasicBinder', level);
        }
    }

    function update(logger, level) {
        console.log("logger:" + logger + ", level:" + level);
        $.ajax({
            url: '${createLink(controller: 'logger',action: 'update')}',
            data: {
                logger: logger,
                level: level
            }
        });
    }

    // 向服务端发起测试请求，服务端会在LoggerController进行日志输出测试
    function test() {
        $.ajax({
            url: '${createLink(controller: 'logger',action: 'test')}'
        });
    }

    function save() {
        $.ajax({
            url: '${createLink(controller: 'logger',action: 'save')}'
        });
    }
</script>
</body>
</html>